﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using ArkanoidX.Objects;

namespace ArkanoidX.RacketStates
{
    public class Fighter : FreeState
    {
        public Shots _shots;
        public TimeSpan _lastShotGameTime = default(TimeSpan);
        public bool _spacePressedLastUpdate = false;

        public const int _kFireInterval = 100;    /* miliseconds */
        Game _game;

        RacketStateHelper _stateHelper;

        public Fighter(Game game, Vector2 size, Rectangle regionInSource,
            Rectangle reachableArea, Shots shots)
            : base(game, size, regionInSource, reachableArea)
        {
            base.Texture = game.Content.Load<Texture2D>("racket2");
            this._shots = shots;
            _stateHelper = new RacketStateHelper(size);
            _game = game;
        }

        public Fighter(Game game, Vector2 size, Rectangle reachableArea, Shots shots)
            : base(game, size, reachableArea)
        {
            base.Texture = game.Content.Load<Texture2D>("racket2");
            this._shots = shots;
            _stateHelper = new RacketStateHelper(size);
            _game = game;
        }

        public Fighter(Game game, Rectangle reachableArea, Shots shots)
            : base(game, reachableArea)
        {
            base.Texture = game.Content.Load<Texture2D>("racket2");
            this._shots = shots;
            _stateHelper = new RacketStateHelper(new Vector2(base.Texture.Bounds.Width,
                base.Texture.Bounds.Height));
            _game = game;
        }

        public override TimeSpan LifeSpan
        {
            get
            {
                return new TimeSpan(1, 0, 3);
            }
        }

        public override bool IsFree
        {
            get
            {
                return true;
            }
        }

        public override void Initiate(Racket.State lastState)
        {
            _stateHelper.Initiate(lastState, this);
            base.Initiate(lastState);
        }

        public override void FadingInUpdate(Racket racket, GameTime gameTime, Racket.State fadingOutState)
        {
            _stateHelper.FadingInUpdate(racket, gameTime, fadingOutState, this);
        }

        // Do update according to the state of the ship
        public override void Update(Racket racket, GameTime gameTime)
        {
            PreUpdate(racket, gameTime);

            if (!this.IsActive)
            {
                return;
            }

            KeyboardState keyboard = Keyboard.GetState();

            if (keyboard.IsKeyDown(Keys.Space))
            {   // fire
                TimeSpan current = gameTime.TotalGameTime;
                TimeSpan dur = current - _lastShotGameTime;

                if (!_spacePressedLastUpdate || dur.TotalMilliseconds > _kFireInterval)
                {
                    Vector2 velocity = new Vector2(0.3f * (float)Math.Sin(_angle),
                        -0.3f * (float)Math.Cos(_angle));
                    _shots.Generate(racket.Position, velocity);
                    _lastShotGameTime = gameTime.TotalGameTime;
                }

                ((MainGame)_game).SoundResources.PlayShoot();

                _spacePressedLastUpdate = true;
            }
            else
            {
                _spacePressedLastUpdate = false;
            }

            _angle = 0.0f;

            if (keyboard.IsKeyDown(Keys.Left))
            {
                racket.Position.X -= 15;
                _angle = 0.1f;
            }

            if (keyboard.IsKeyDown(Keys.Right))
            {
                racket.Position.X += 15;
                _angle = -0.1f;
            }

            if (keyboard.IsKeyDown(Keys.Up))
            {
                racket.Position.Y -= 5;
            }

            if (keyboard.IsKeyDown(Keys.Down))
            {
                racket.Position.Y += 15;
            }

            // Keep the racket inside the screen
            if (racket.Position.X < base._reachableArea.Left + base._halfSize.X)
            {
                racket.Position.X = base._reachableArea.Left + base._halfSize.X;
            }

            if (racket.Position.X + base._halfSize.X > base._reachableArea.Right)
            {
                racket.Position.X = base._reachableArea.Right - base._halfSize.X;
            }

            if (racket.Position.Y < base._reachableArea.Top + base._halfSize.Y)
            {
                racket.Position.Y = base._reachableArea.Top + base._halfSize.Y;
            }

            if (racket.Position.Y > base._reachableArea.Bottom - base._halfSize.Y)
            {
                racket.Position.Y = base._reachableArea.Bottom - base._halfSize.Y;
            }
        }
    }

}
